package com.hualan.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.mapper.PsychologyTestMapper;
import com.hualan.sfzy.mapper.PsychologyTestTitleMapper;
import com.hualan.sfzy.model.entity.Answer;
import com.hualan.sfzy.model.entity.PsychologyTest;
import com.hualan.sfzy.model.entity.PsychologyTestTitle;
import com.hualan.sfzy.service.BaseService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PsychologyTestServiceImpl implements BaseService<PsychologyTest> {
    @Autowired
    private PsychologyTestMapper psychologyTestMapper;
    @Autowired
    private PsychologyTestTitleMapper TitleMapper;
    /*添加*/
    @Override
    public int add(PsychologyTest entity) {
        return psychologyTestMapper.insert(entity);
    }

    /*修改*/
    @Override
    public int edit(PsychologyTest entity) {
        return psychologyTestMapper.update(entity);
    }

    /*根据id删除单个*/
    @Override
    public int remove(Integer id) {
        return psychologyTestMapper.delete(id);
    }

    /*根据id批量删除*/
    @Override
    public int removeBatch(Integer[] ids) {
        return psychologyTestMapper.deleteBatch(ids);
    }

    /*查询所有*/
    @Override
    public List<PsychologyTest> find() {
        return psychologyTestMapper.select(null);
    }

    /*分页查询所有*/
    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, PsychologyTest entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<PsychologyTest> list = psychologyTestMapper.select(entity);
        PageInfo<PsychologyTest> pageInfo = new PageInfo<>(list); // 分页信息对象
        Map<String, Object> map = new HashMap<>();
        // map存储当前页数据、总页数、总记录数
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    /*根据id查询单个*/
    @Override
    public PsychologyTest findById(Integer id) {
        return psychologyTestMapper.selectById(id);
    }


    /*根据id查询单个(多表)*/
    public PsychologyTest findByIdDuo(Integer id) {
        return psychologyTestMapper.selectByIdDuo(id);
    }

    /*根据id修改(单表)*/
    public int editById(PsychologyTest record) {
        return psychologyTestMapper.updateById(record);
    }


    /*多表查询*/
    public Map<String, Object> selectDuo(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PsychologyTest> list = psychologyTestMapper.selectDuo();
        PageInfo<PsychologyTest> pageInfo = new PageInfo<>(list); // 分页信息对象
        Map<String, Object> map = new HashMap<>();
        // map存储当前页数据、总页数、总记录数
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    /*根据title(模糊)，introduction(模糊)，classifyName多条件查询*/
    public Map<String, Object> findByCondition(Integer pageNum, Integer pageSize, PsychologyTest entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<PsychologyTest> list = psychologyTestMapper.findByCondition(entity);
        PageInfo<PsychologyTest> pageInfo = new PageInfo<>(list); // 分页信息对象
        Map<String, Object> map = new HashMap<>();
        // map存储当前页数据、总页数、总记录数
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    /*查询所有关联的分类名称*/
    public List<PsychologyTest> selectAllClassify() {
        return psychologyTestMapper.selectAllClassify();
    }

    /*查询所有id 和对应的 title*/
    public List<PsychologyTest> selectAllIdAndTitle() {
        return psychologyTestMapper.selectAllIdAndTitle();
    }

    /*根据testid查找对应的题目和选项*/
    public List<PsychologyTest> selectAllTitleAndOptions(Integer testId) {
        return psychologyTestMapper.selectAllTitleAndOptions(testId);
    }



}
